<template>
  <div class="search-content wrap-apply" :style="{top: GLOBAL.currentEnvironment ? '0' : ''}" v-show="showSearchTag">
    <Search ref="topSearch" class="search" placeholder="商品名称|商品编号|规格" :isAdd="false" :isScreen="false" :isType="false"></Search>
    <div class="searchBox">
      <van-cell-group style="margin-top:0.1rem">
        <p class="label FormTit"><span></span>任务工序</p>
        <van-field-checkbox :supportClear='true' label="" placeholder="请选择" v-model="processSelectArr" :columns="messSingleProcessTaskStaticsDtoList" label-width="100"
          :option="{label:'text',value:'value'}" @confirm='processSelectConfirm' />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>报工状态</p>
        <van-field-select-picker label="" placeholder="请选择" :supportClear='true' v-model="filterForm.reportStatus" :columns="reportSelectList"
          :option="{ label: 'text', value: 'value' }" @confirm="selectReport" @prePopShow="showAccountDialog" />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>生产状态</p>
        <van-field-select-picker label="" placeholder="请选择" :supportClear='true' v-model="filterForm.orderStatus" :columns="statusList"
          :option="{ label: 'text', value: 'value' }" @confirm="selectProduct" @prePopShow="showAccountDialog" />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>工单编号</p>
        <van-field v-model="filterForm.orderCode" placeholder="请输入" />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>生产物料</p>
        <van-field v-model="filterForm.goodsName" placeholder="请输入" />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>计划开始时间</p>
        <div :class="filterForm.planStartTimeArr?'haveUnitBox':''">
          <van-field :is-link='filterForm.planStartTimeArr?false:true' readonly name="picker" v-model="filterForm.planStartTimeArr" @click="planStart_beginTime = true"
            placeholder="请选择">
          </van-field>
          <img src="~@/assets/img/filedClear.png" v-if="filterForm.planStartTimeArr" alt="" @click.stop='clickStartClose' class="filedClear">
        </div>
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label FormTit"><span></span>计划完成时间</p>
        <div :class="filterForm.planOverTimeArr?'haveUnitBox':''">
          <van-field :is-link='filterForm.planOverTimeArr?false:true' readonly name="picker" v-model="filterForm.planOverTimeArr" @click="planOver_beginTime = true"
            placeholder="请选择">
          </van-field>
          <img src="~@/assets/img/filedClear.png" v-if="filterForm.planOverTimeArr" alt="" @click.stop='clickEndClose' class="filedClear">
        </div>
      </van-cell-group>
    </div>
    <div class="search-bottom">
      <span class="btncancel" @click="btnCancel">重置</span>
      <span class="btnconfirm" @click="btnConfirm">确定</span>
    </div>
    <!--计划开始时间—开始时间 -->
    <van-popup v-model="planStart_beginTime" position="bottom" :close-on-click-overlay='false'>
      <van-datetime-picker @confirm="confirmPlanStartBeginTime" v-model="startTimeStart" @cancel="cancelPlanStartBeginTime" type="date" title="开始时间"
        :formatter="formatter" />
    </van-popup>
    <!--计划开始时间—结束时间 -->
    <van-popup v-model="planStart_endTime" position="bottom" :close-on-click-overlay='false'>
      <van-datetime-picker @confirm="confirmPlanStartEndTime" @cancel="cancelPlanStartEndTime" type="date" title="结束时间" :min-date="startEndMinTIme"
        :formatter="formatter" />
    </van-popup>

    <!--计划完成时间—开始时间 -->
    <van-popup v-model="planOver_beginTime" position="bottom" :close-on-click-overlay='false'>
      <van-datetime-picker @confirm="confirmPlanOverBeginTime" v-model="overTimeStart" @cancel="cancelPlanOverBeginTime" type="date" title="开始时间"
        :formatter="formatter" />
    </van-popup>
    <!--计划完成时间—结束时间 -->
    <van-popup v-model="planOver_endTime" position="bottom" :close-on-click-overlay='false'>
      <van-datetime-picker @confirm="confirmPlanOverEndTime" @cancel="cancelPlanOverEndTime" type="date" title="结束时间" :min-date="overEndMinTIme" :formatter="formatter" />
    </van-popup>
  </div>
</template>

<script>
const formP = {
  applicantId: "",
  operatorId: "",
  applyTimeBegin: "",
  applyTimeEnd: "",
};
import VanFieldCheckbox from "@/components/ActionSheet/VanFieldCheckbox";
import ContactPickers from "@/components/choiceuse/ContactPickers";
import upload from "@/components/upload/upload";
import VanFieldSelectPicker from "@/components/ActionSheet/VanFieldSelectPicker";
import moment from "moment";
import Search from "@/components/search";
import { processTaskCompletionStatics } from "@/api/mobile";
export default {
  name: "search",
  components: {
    ContactPickers,
    upload,
    Search,
    VanFieldSelectPicker,
    VanFieldCheckbox,
  },
  data() {
    return {
      startTimeStart: new Date(),
      overTimeStart: new Date(),
      showSearchTag: false,
      planStart_beginTime: false,
      planStart_endTime: false,
      planOver_beginTime: false,
      planOver_endTime: false,
      startEndMinTIme: new Date(2020, 1, 1),
      overEndMinTIme: new Date(2020, 1, 1),

      /***** */
      filterForm: {
        planStartTimeArr: "",
        planOverTimeArr: "",
      },
      messSingleProcessTaskStaticsDtoList: [],
      reportSelectList: [
        // {
        //   text: "请选择",
        //   value: "",
        // },
        {
          text: "可报工",
          value: "yes",
        },
        {
          text: "不可报工",
          value: "no",
        },
      ],
      statusList: [
        // {
        //   text: "请选择",
        //   value: "",
        // },
        {
          text: "未开始",
          value: "notStarted",
        },
        {
          text: "生产中",
          value: "inProduction",
        },
        {
          text: "已作废",
          value: "abolished",
        },
        {
          text: "已完成",
          value: "completed",
        },
      ],
      realForm: {},
      processSelectArr: [],
    };
  },
  computed: {},
  mounted() {},
  created() {
    this.getLeftData();
  },
  methods: {
    clickStartClose() {
      this.startTimeStart = new Date();
      this.filterForm.planStartTimeBegin = "";
      this.filterForm.planStartTimeEnd = "";
      this.filterForm.planStartTimeArr = "";
      this.$forceUpdate();
    },
    clickEndClose() {
      this.overTimeStart = new Date();
      this.filterForm.planEndTimeBegin = "";
      this.filterForm.planEndTimeEnd = "";
      this.filterForm.planOverTimeArr = "";
      this.$forceUpdate();
    },
    showModal(keyword, dropdownObj) {
      this.showSearchTag = true;
      this.$nextTick(() => {
        this.$refs.topSearch.value = keyword;
        this.filterForm = JSON.parse(JSON.stringify(dropdownObj));
        this.processSelectArr = this.filterForm.processId
          ? this.filterForm.processId.split(",")
          : [];
      });
    },
    //显示弹框选择
    showAccountDialog() {},
    processSelectConfirm(proceArr) {
      if (proceArr) {
        this.processSelectArr = proceArr;
      } else {
        this.processSelectArr = [];
      }
    },
    selectReport(itemReport) {
      if (itemReport) {
        this.filterForm.reportStatus = itemReport.value;
      } else {
        this.filterForm.reportStatus = "";
      }
    },
    selectProduct(itemProduct) {
      if (itemProduct) {
        this.filterForm.orderStatus = itemProduct.value;
      } else {
        this.filterForm.orderStatus = "";
      }
    },
    getLeftData() {
      let params = {
        type: "production",
      };
      processTaskCompletionStatics(params).then((res) => {
        if (res.success) {
          let resultList = res.result.messSingleProcessTaskStaticsDtoList;
          this.messSingleProcessTaskStaticsDtoList = [];
          resultList = resultList.filter((item) => {
            return item.processName && item.processName != "全部";
          });
          for (let item of resultList) {
            let resultObj = {
              text: item.processName,
              value: item.processId,
            };
            this.messSingleProcessTaskStaticsDtoList.push(resultObj);
          }
        } else {
          this.$toast(res.message);
        }
      });
    },
    // 选择计划开始时间—开始时间
    confirmPlanStartBeginTime(date) {
      this.startEndMinTIme = date;
      this.filterForm.planStartTimeBegin = moment(date).format("YYYY-MM-DD");
      this.filterForm.planStartTimeEnd = ""; // 切换开始时间后直接置空结束时间重新选择
      this.planStart_beginTime = false;
      this.planStart_endTime = true;
    },
    // 取消计划开始时间—开始时间
    cancelPlanStartBeginTime() {
      this.planStart_beginTime = false;
    },
    // 选择计划开始时间—结束时间
    confirmPlanStartEndTime(date) {
      this.filterForm.planStartTimeEnd = moment(date).format("YYYY-MM-DD");
      this.filterForm.planStartTimeArr =
        this.filterForm.planStartTimeBegin +
        "~" +
        this.filterForm.planStartTimeEnd;
      this.planStart_endTime = false;
    },
    // 取消计划开始时间—结束时间
    cancelPlanStartEndTime() {
      this.filterForm.planStartTimeBegin = "";
      this.startTimeStart = new Date();
      this.planStart_endTime = false;
    },

    confirmPlanOverBeginTime(date) {
      this.overEndMinTIme = date;
      this.filterForm.planEndTimeBegin = moment(date).format("YYYY-MM-DD");
      this.filterForm.planEndTimeEnd = ""; // 切换开始时间后直接置空结束时间重新选择
      this.planOver_beginTime = false;
      this.planOver_endTime = true;
    },
    cancelPlanOverBeginTime() {
      this.planOver_beginTime = false;
    },
    confirmPlanOverEndTime(date) {
      this.filterForm.planEndTimeEnd = moment(date).format("YYYY-MM-DD");
      this.filterForm.planOverTimeArr =
        this.filterForm.planEndTimeBegin + "~" + this.filterForm.planEndTimeEnd;
      this.planOver_endTime = false;
    },
    cancelPlanOverEndTime() {
      this.overTimeStart = new Date();
      this.filterForm.planEndTimeBegin = "";
      this.planOver_endTime = false;
    },
    formatter(type, val) {
      if (type === "year") {
        return val + "年";
      }
      if (type === "month") {
        return val + "月";
      }
      if (type === "day") {
        return val + "日";
      }
      if (type === "hour") {
        return val + "时";
      }
      if (type === "minute") {
        return val + "分";
      }
      return val;
    },

    btnCancel() {
      this.$refs.topSearch.value = "";
      this.filterForm = {
        processId: null,
        reportStatus: null,
        orderStatus: null,
      };
      this.processSelectArr = [];
    },
    btnConfirm() {
      this.filterForm.processId = this.processSelectArr.join(",");
      let sureObj = {
        ...this.filterForm,
        keyword: this.$refs.topSearch.value,
      };
      if (
        sureObj.planStartTimeBegin &&
        sureObj.planStartTimeBegin.indexOf("00:00:00") < 0
      ) {
        sureObj.planStartTimeBegin =
          sureObj.planStartTimeBegin + " " + "00:00:00";
      }
      if (
        sureObj.planStartTimeEnd &&
        sureObj.planStartTimeEnd.indexOf("23:59:59") < 0
      ) {
        sureObj.planStartTimeEnd = sureObj.planStartTimeEnd + " " + "23:59:59";
      }
      if (
        sureObj.planEndTimeBegin &&
        sureObj.planEndTimeBegin.indexOf("00:00:00") < 0
      ) {
        sureObj.planEndTimeBegin = sureObj.planEndTimeBegin + " " + "00:00:00";
      }
      if (
        sureObj.planEndTimeEnd &&
        sureObj.planEndTimeEnd.indexOf("23:59:59") < 0
      ) {
        sureObj.planEndTimeEnd = sureObj.planEndTimeEnd + " " + "23:59:59";
      }
      if (!sureObj.processId && sureObj.processId != "") {
        sureObj.processId = "";
      }
      if (!sureObj.reportStatus && sureObj.reportStatus != "") {
        sureObj.reportStatus = "";
      }
      if (!sureObj.orderStatus && sureObj.orderStatus != "") {
        sureObj.orderStatus = "";
      }
      this.showSearchTag = false;
      this.$emit("getCategory", sureObj);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
.search-content {
  //  padding-bottom: 0.5rem;
  position: fixed;
  top: 0.35rem;
  width: 100%;
  height: 100vh;
  background: #f5f5f5;
  z-index: 99;
  .formgroup {
    background: #ffffff;
    // padding: 0 0.16rem;
    margin-top: 0.1rem;
    margin-bottom: 0.2rem;
    padding-top: 0.1rem;

    .title {
      width: 0.58rem;
      height: 0.14rem;
      font-size: 0.15rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
      line-height: 0.05rem;
      margin-left: 0.16rem;
    }
  }
  .search-bottom {
    width: 100%;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: 0px;
    padding: 0.06rem 0.11rem;
    font-size: 0.16rem;
    font-weight: 500;
    .btncancel {
      flex: 1;
      background: #f6f6f6;
      border: 0.01rem solid rgba(25, 31, 37, 0.12);
      box-sizing: border-box;
      height: 0.39rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.03rem;
      margin-right: 0.11rem;
      color: #191f25;
    }
    .btnconfirm {
      flex: 1;
      background: #1890ff;
      box-sizing: border-box;
      height: 0.39rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.03rem;
      color: #ffffff;
    }
  }
}
.bottomLine {
  height: 0.01rem;
  background-color: #f0f0f0;
  margin: 0 0.16rem;
}
.searchBox {
  height: calc(100vh - 1.6rem);
  overflow-y: auto;
}
.haveUnitBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding-right: 0.16rem;
  /deep/ .van-cell {
    padding-right: 0;
    width: 2.6rem;
  }
  /deep/ .van-field__control {
    max-width: 3rem;
  }
}
</style>